Passed
Pull Request — filestream (#169)
by
unknown
01:52
created

write.ts ➔ writeAsync   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
1
import { WriteTags } from "../types/Tags"
2
import { WriteCallback, WriteOptions } from "../types/write"
3
import { create }  from "./create"
4
import { removeTagsFromBuffer } from "./remove"
5
import { isFunction, isString, validateString } from "../util"
6
import { writeId3TagToFile, writeId3TagToFileSync } from "../file-write"
7
8
/**
9
 * Replaces any existing tags with the given tags in the given buffer.
10
 * Throws in case of error.
11
 * @public
12
 */
13
export function write(tags: WriteTags, buffer: Buffer): Buffer
14
15
/**
16
 * Replaces synchronously any existing tags with the given tags in the
17
 * specified file.
18
 * Throws in case of error.
19
 * @public
20
 */
21
export function write(
22
    tags: WriteTags,
23
    filepath: string,
24
    options?: WriteOptions
25
): void
26
27
/**
28
 * Replaces asynchronously any existing tags with the given tags in the
29
 * specified file.
30
 * @public
31
 */
32
export function write(
33
    tags: WriteTags, filepath: string, callback: WriteCallback
34
): void
35
36
export function write(
37
    tags: WriteTags,
38
    filebuffer: string | Buffer,
39
    optionsOrCallback?: WriteOptions | WriteCallback,
40
    maybeCallback?: WriteCallback
41
): Buffer | void {
42
    const options =
43
        (isFunction(optionsOrCallback) ? {} : optionsOrCallback) ?? {}
44
    const callback =
45
        isFunction(optionsOrCallback) ? optionsOrCallback : maybeCallback
46
47
    if (isFunction(callback)) {
48
        writeInFile(tags, validateString(filebuffer), options, callback)
49
        return
50
    }
51
    if (isString(filebuffer)) {
52
        return writeInFileSync(tags, filebuffer, options)
53
    }
54
    return writeInBuffer(tags, filebuffer)
55
}
56
57
// New API
58
59
/**
60
 * Replaces any existing tags with the given tags in the given buffer.
61
 * Throws in case of error.
62
 * @public
63
 */
64
export function writeInBuffer(tags: WriteTags, buffer: Buffer): Buffer {
65
    const id3Tag = create(tags)
66
    const bufferWithoutId3Tag = removeTagsFromBuffer(buffer) || buffer
67
    return Buffer.concat([id3Tag, bufferWithoutId3Tag])
68
}
69
70
/**
71
 * Replaces synchronously any existing tags with the given tags in the
72
 * specified file.
73
 * Throws in case of error.
74
 * @public
75
 */
76
export function writeInFileSync(
77
    tags: WriteTags,
78
    filepath: string,
79
    options: WriteOptions = {}
80
): void {
81
    const id3Tag = create(tags)
82
    writeId3TagToFileSync(filepath, id3Tag, options)
83
}
84
85
/**
86
 * Replaces asynchronously any existing tags with the given tags in the
87
 * specified file.
88
 * @public
89
 */
90
export function writeInFile(
91
    tags: WriteTags,
92
    filepath: string,
93
    callback: WriteCallback
94
): void
95
96
/**
97
 * Replaces asynchronously any existing tags with the given tags in the
98
 * specified file.
99
 * @public
100
 */
101
export function writeInFile(
102
    tags: WriteTags,
103
    filepath: string,
104
    options: WriteOptions,
105
    callback: WriteCallback
106
): void
107
108
/**
109
 * Replaces asynchronously any existing tags with the given tags in the
110
 * specified file.
111
 * @public
112
 */
113
export function writeInFile(
114
    tags: WriteTags,
115
    filepath: string,
116
    optionsOrCallback: WriteOptions | WriteCallback,
117
    maybeCallback?: WriteCallback
118
): void {
119
    const options =
120
        (isFunction(optionsOrCallback) ? {} : optionsOrCallback) ?? {}
121
    const callback =
122
        isFunction(optionsOrCallback) ? optionsOrCallback : maybeCallback
123
124
    const id3Tag = create(tags)
125
    writeId3TagToFile(filepath, id3Tag, options, callback ?? (() => { /* */ }))
126
}
127